package com.lcf.common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.system.SystemUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import lombok.SneakyThrows;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 文档：http://deepoove.com/poi-tl/
 */
public class WordUtils {


    public static void main(String[] args) {
        String file = "C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_bij0qf56w0ib21\\FileStorage\\File\\2022-11\\delayOffset.json";
        String mimeType = FileUtil.getMimeType(file);
        Console.log(mimeType);
    }

    /**
     * 文本测试
     */
    public static class Text {

        @SneakyThrows
        public static void main(String[] args) {
            // 	compile 编译模板
            XWPFTemplate template = XWPFTemplate.compile(ResourceUtil.getStream("word/template1.docx")).render(
                    // 	render 渲染数据
                    new HashMap<String, Object>() {{
                        put("title", "Word模板引擎 - 文本填充");
                    }});
            //write 输出到流
            String outPath = SystemUtil.getUserInfo().getHomeDir() + "out.docx";
            template.writeAndClose(new FileOutputStream(outPath));
            //打开生成的文件
            Desktop.getDesktop().open(new File(outPath));
        }
    }

    /**
     * 表格渲染测试
     */
    public static class Table {

        @SneakyThrows
        public static void main(String[] args) {
            LoopRowTableRenderPolicy tableRenderPolicy = new LoopRowTableRenderPolicy();
            Configure config = Configure.builder()
                    .bind("userList", tableRenderPolicy)
                    .build();
            //模拟数据
            String title = "人员信息表";
            String description = "该数据为模拟数据！";
            List<User> userList = Arrays.asList(
                    new User("张三", 24, "05-23", "男", "群众", "汉"),
                    new User("李四", 35, "07-06", "女", "群众", "傣族"),
                    new User("王麻子", 18, "01-12", "女", "共青团员", "维吾尔族"));
            //编译模板
            XWPFTemplate template = XWPFTemplate.compile(ResourceUtil.getStream("word/template2.docx"), config).render(
                    new HashMap<String, Object>() {{
                        put("userList", userList);
                        put("title", title);
                        put("description", description);
                    }}
            );
            //输出
            String outPath = SystemUtil.getUserInfo().getHomeDir() + "out.docx";
            template.writeAndClose(new FileOutputStream(outPath));
            //打开生成的文件
            Desktop.getDesktop().open(new File(outPath));
        }

    }

}
